package com.clhbolg.strategy.context;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.clhbolg.strategy.UploadStrategy;
import com.ruoyi.common.enums.UploadModeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.PostConstruct;
import java.io.InputStream;
import java.util.Map;

@Service
@DS("slave")
public class UploadStrategyContext {

    @Autowired
    Environment environment;

    @PostConstruct
    public void init(){
        uploadMode=  environment.getProperty("upload.mode");
    }

    private String uploadMode;

    @Autowired
    private Map<String, UploadStrategy> uploadStrategyMap;

    public String executeUploadStrategy(MultipartFile file, String path) {
        return uploadStrategyMap.get(UploadModeEnum.getStrategy(uploadMode)).uploadFile(file, path);
    }

    public String executeUploadStrategy(String fileName, InputStream inputStream, String path) {
        return uploadStrategyMap.get(UploadModeEnum.getStrategy(uploadMode)).uploadFile(fileName, inputStream, path);
    }


    public void executeDeleteStrategy(String fileName) {
         uploadStrategyMap.get(UploadModeEnum.getStrategy(uploadMode)).deleteFile(fileName);
    }

}
